package com.parameterreplacement;

import com.google.gson.Gson;
import com.parameterreplacement.entity.Data;
import com.parameterreplacement.entity.DepartmentLeaders;
import com.parameterreplacement.entity.ResponseData;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 刘欣
 * @createTime 2023/12/21 10:13
 * @description
 */
public class RestTemplateExample {

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://127.0.0.1:90/openapi/sys-org/v2/element/findPersonByIds";

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("fdLoginName", "jms001");

        // 创建请求实体
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);

        // 发送请求并获取响应
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        Gson gson = new Gson();
        DepartmentLeaders departmentLeaders = new DepartmentLeaders();
        ResponseData responseData = gson.fromJson(responseEntity.getBody(), ResponseData.class);
        if (responseData.getData()!=null&& responseData.getData().size()>0){
            List<Data> data = responseData.getData();
            Data mkUser = data.get(0);
            if (mkUser!=null){
                departmentLeaders.setFdId(mkUser.getFdId());
                departmentLeaders.setFdOrgType(mkUser.getFdOrgType());
                departmentLeaders.setFdName(mkUser.getFdName());
                departmentLeaders.setFdLoginName(mkUser.getFdAccount().getFdLoginName());
                departmentLeaders.setFdInner(mkUser.isFdInner());
                departmentLeaders.setEosShowType(mkUser.isEosShowType());
                departmentLeaders.setFdIsAvailable(mkUser.isFdIsAvailable());
            }
        }
        // 输出响应结果
        System.out.println(responseEntity.getBody());
        System.out.println(responseData);
        System.out.println(departmentLeaders);
    }
}

